// packageJ/chain-store/mycomponent/posCart/posCart.js
const app = getApp();
const cartPost = require('../cart');
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
Component({
  behaviors: [skinBehavior, cartPost],
  /**
   * 组件的属性列表
   */
  properties: {
    showGood: {
      type: Boolean,
      value: false
    },
    carts: {
      type: null
    },
    goodsCartsTotal: {
      type: null
    },
    goodsCartsTotal_wxs: {
      type: null
    },
    cartsNum: {
      type: null
    },
    cartsTotal: {
      type: null
    },
    store_id: {
      type: null
    },
    good_id: {
      type: null
    },
    child: {
      type: String,
      value: ''
    },
    get_type: {
      type: null
    }
  },
  lifetimes: {
    attached() {
      this.getiPnoneBottom();
      wx.getStorage({
        key: 'langIndex',
        success: (res) => {
          this.setData({
            'language': res.data.en
          });
        }
      });
    },
  },
  observers: {
    'good_id': function (nval) {
      console.log(nval, 'poscart 1111111111111111111')
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    showCarList: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getiPnoneBottom() {
      let that = this;
      try {
        var value = wx.getStorageSync('iPhoneBottom');
        if (this.data.child == 'search') {
          this.setData({
            iPnoneBottomHeight: '48px',
            iPnoneBottomHeight_show: '48px'
          })
          return
        }
        if (value) {
          // that.setData({
          //   iPnoneBottomBol: true
          // });
          // Do something with return value
          this.setData({
            iPnoneBottomHeight: '100px',
            iPnoneBottomHeight_show: '32px'
          })
        } else {
          this.setData({
            iPnoneBottomHeight: '50px',
            iPnoneBottomHeight_show: '32px'
          })
        }
      } catch (e) {
        // Do something when catch error
      }
    },
    closedCarList() {
      this.setData({
        showCarList: false
      })
    },
    updateCart() {
      this.triggerEvent('updateCart')
    },
    close_yz_cartList_popup() {
      this.setData({
        showCarList: false
      })
    },
    showCarListTap() {
      this.setData({
        showCarList: true
      })
    },
    goodsOrder(e) {
      let that = this;
      app._getNetWork({
        showToastIn: false,
        url: app.getNetAddresss("member.member.isValidatePageGoods"),
        success: async (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            // if (that.data.goodsInfo.has_one_invite_page && that.data.goodsInfo.has_one_invite_page.status === 1 && res.data.is_invite === 0) {
            //   wx.navigateTo({
            //     url: "/packageC/member/visit/Inviter/Inviter?fromGood=" + 1,
            //   });
            //   return;
            // } else {
            //判断是否上面安装的商品
            let confirmFlag = await this.isHasDifferType(that.data.carts);
            console.log('ddddddddddd',confirmFlag)
            if (!confirmFlag) {
              return;
            }
            let sum = 0;
            that.data.carts.forEach((val) => {
              sum += val.total;
            });
            if (sum == 0) {
              app.tips('请选择商品')
              return;
            }
            console.log(that.data.carts,'ddddddddddddddsa')
            if (Object.keys(that.data.carts).length == 0) {
              app.tips('请选择商品')
              return;
            } else {
              wx.navigateTo({
                url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=store&store_id=" + that.data.store_id + '&get_type='+this.data.get_type,
              });
            }
            // }
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
      });
    },
    async isHasDifferType(data) {
      let open_state = 0; //有安装服务的商品
      let clone_state = 0; //没安装服务的商品
      let plugin_name = "";
      for (let item in data) {
        if (item.live_install && item.live_install.open_state == 1) {
          open_state += 1;
          plugin_name = item.live_install.plugin_name;
        } else {
          clone_state += 1;
        }
      }
      if (open_state != 0 && clone_state != 0) {
        let confirm = await app.confirm(`普通商品与${plugin_name}商品一同下单将无法享受${plugin_name}，是否继续下单？`);
        return confirm;
      }
      return true;
    },
  }
})